home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows 95 with MFC / Programming Windows 95 with MFC (Microsoft Programming Series)(097-0001465)(1996).iso / CODE / Chap02 / GdiDemo1 / GdiDemo1.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-05  |  4.4 KB  |  174 lines

  1. //***********************************************************************
  2. //
  3. //  GdiDemo1.cpp
  4. //
  5. //***********************************************************************
  6.  
  7. #include <afxwin.h>
  8. #include "GdiDemo1.h"
  9.  
  10. CMyApp myApp;
  11.  
  12. /////////////////////////////////////////////////////////////////////////
  13. // CMyApp member functions
  14.  
  15. BOOL CMyApp::InitInstance ()
  16. {
  17.     m_pMainWnd = new CMainWindow;
  18.     m_pMainWnd->ShowWindow (m_nCmdShow);
  19.     m_pMainWnd->UpdateWindow ();
  20.     return TRUE;
  21. }
  22.  
  23. /////////////////////////////////////////////////////////////////////////
  24. // CMainWindow message map and member functions
  25.  
  26. BEGIN_MESSAGE_MAP (CMainWindow, CFrameWnd)
  27.     ON_WM_CREATE ()
  28.     ON_WM_PAINT ()
  29. END_MESSAGE_MAP ()
  30.  
  31. CMainWindow::CMainWindow ()
  32. {
  33.     Create (NULL, "GdiDemo1");
  34. }
  35.  
  36. int CMainWindow::OnCreate (LPCREATESTRUCT lpcs)
  37. {
  38.     if (CFrameWnd::OnCreate (lpcs) == -1)
  39.         return -1;
  40.     
  41.     TEXTMETRIC tm;
  42.     CClientDC dc (this);
  43.     dc.GetTextMetrics (&tm);
  44.     m_cxChar = tm.tmAveCharWidth;
  45.     m_cyChar = tm.tmHeight;
  46.     return 0;
  47. }
  48.  
  49. void CMainWindow::OnPaint ()
  50. {
  51.     CPaintDC dc (this);
  52.                 
  53.     ShowPenStyles (&dc, m_cxChar * 2, m_cyChar);   
  54.     ShowPenWidths (&dc, m_cxChar * 2, m_cyChar * 15);
  55.     ShowBrushStyles (&dc, m_cxChar * 2, m_cyChar * 27);
  56. }
  57.  
  58. void CMainWindow::ShowPenStyles (CDC* pDC, int x, int y)
  59. {
  60.     static struct STYLES styles[] = {
  61.         PS_SOLID,       "PS_SOLID",
  62.         PS_DASH,        "PS_DASH",
  63.         PS_DOT,         "PS_DOT",
  64.         PS_DASHDOT,     "PS_DASHDOT",
  65.         PS_DASHDOTDOT,  "PS_DASHDOTDOT",
  66.         PS_NULL,        "PS_NULL",
  67.         PS_INSIDEFRAME, "PS_INSIDEFRAME"
  68.     };
  69.  
  70.     pDC->SetTextColor (RGB (0, 0, 0));
  71.     pDC->TextOut (x, y, "Pen Styles");
  72.  
  73.     int dy = (m_cyChar * 3) / 2;
  74.     int x1 = x + (m_cxChar * 2);
  75.     int x2 = x + (m_cxChar * 22);
  76.     int x3 = x + (m_cxChar * 46);
  77.  
  78.     CPen* pOldPen;
  79.     pDC->SetTextColor (RGB (255, 0, 0));
  80.  
  81.     for (int i=0; i<7; i++) {
  82.         y += dy;
  83.         pDC->TextOut (x1, y, styles[i].szStyleName);
  84.  
  85.         CPen pen (styles[i].nStyle, 1, RGB (255, 0, 0));
  86.         pOldPen = pDC->SelectObject (&pen);
  87.  
  88.         pDC->MoveTo (x2, y + (m_cyChar / 2));
  89.         pDC->LineTo (x3, y + (m_cyChar / 2));
  90.  
  91.         pDC->SelectObject (pOldPen);
  92.     }
  93. }
  94.  
  95. void CMainWindow::ShowPenWidths (CDC* pDC, int x, int y)
  96. {
  97.     static int nPenWidths[] = { 2, 8, 16, 24 };
  98.  
  99.     pDC->SetTextColor (RGB (0, 0, 0));
  100.     pDC->TextOut (x, y, "Pen Widths");
  101.  
  102.     int dy = m_cyChar * 2;
  103.     int x1 = x + (m_cxChar * 2);
  104.     int x2 = x + (m_cxChar * 22);
  105.     int x3 = x + (m_cxChar * 46);
  106.  
  107.     CPen* pOldPen;
  108.     CString strDescription;
  109.     pDC->SetTextColor (RGB (0, 0, 255));
  110.  
  111.     for (int i=0; i<4; i++) {
  112.         y += dy;
  113.         strDescription.Format ("%d Pixels", nPenWidths[i]);
  114.         pDC->TextOut (x1, y, strDescription);
  115.  
  116.         CPen pen (PS_SOLID, nPenWidths[i], RGB (0, 0, 255));
  117.         pOldPen = pDC->SelectObject (&pen);
  118.  
  119.         pDC->MoveTo (x2, y + (m_cyChar / 2));
  120.         pDC->LineTo (x3, y + (m_cyChar / 2));
  121.  
  122.         pDC->SelectObject (pOldPen);
  123.     }
  124. }
  125.  
  126. void CMainWindow::ShowBrushStyles (CDC* pDC, int x, int y)
  127. {
  128.     static struct STYLES styles[] = {
  129.         HS_BDIAGONAL,   "HS_BDIAGONAL",
  130.         HS_CROSS,       "HS_CROSS",
  131.         HS_DIAGCROSS,   "HS_DIAGCROSS",
  132.         HS_FDIAGONAL,   "HS_FDIAGONAL",
  133.         HS_HORIZONTAL,  "HS_HORIZONTAL",
  134.         HS_VERTICAL,    "HS_VERTICAL"
  135.     };
  136.  
  137.     pDC->SetTextColor (RGB (0, 0, 0));
  138.     pDC->TextOut (x, y, "Brush Styles");
  139.  
  140.     int dy = m_cyChar * 3;
  141.     int x1 = x + (m_cxChar * 2);
  142.     int x2 = x + (m_cxChar * 22);
  143.     int x3 = x + (m_cxChar * 46);
  144.  
  145.     CBrush* pOldBrush;
  146.  
  147.     for (int i=0; i<6; i++) {
  148.         y += dy;
  149.         pDC->TextOut (x1, y, styles[i].szStyleName);
  150.  
  151.         CRect rect (x2, y - m_cyChar, x3, y + m_cyChar);
  152.         CBrush brush (styles[i].nStyle, RGB (0, 255, 0));
  153.  
  154.         CPoint point (rect.left, rect.top);
  155.         pDC->LPtoDP (&point);
  156.         point.x %= 8;
  157.         point.y %= 8;
  158.         pDC->SetBrushOrg (point);
  159.  
  160.         pOldBrush = pDC->SelectObject (&brush);
  161.         pDC->Rectangle (rect);
  162.  
  163.         pDC->SelectObject (pOldBrush);
  164.     }
  165.  
  166.     y += dy;
  167.     pDC->TextOut (x1, y, "Solid");
  168.  
  169.     CBrush brush (RGB (0, 255, 0));
  170.     pOldBrush = pDC->SelectObject (&brush);
  171.     pDC->Rectangle (x2, y - m_cyChar, x3, y + m_cyChar);
  172.     pDC->SelectObject (pOldBrush);
  173. }
  174.